﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using Moq;

namespace MvcWiki.Test.Repository.FileSystem.WikiPageFileSystemRepository
{
    public class SaveWikiPageFacts
    {
        [Fact]
        public void Saves_WikiPage()
        {
            var wikiNamespaceRepository = new Mock<MvcWiki.Repository.IWikiNamespaceRepository>();

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiNamespaceRepository()).Returns(wikiNamespaceRepository.Object);

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();
            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();

            var wikiPageRepository = new MvcWiki.Repository.FileSystem.WikiPageFileSystemRepository(repositoryFactory.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            var wikiPage = new MvcWiki.Model.WikiPage();

            wikiPageRepository.SaveWikiPage(wikiPage);
        }

        [Fact]
        public void Calls_IUnitOfWork_Saved()
        {
            var wikiPage = new MvcWiki.Model.WikiPage
            {
                Name = "Page",
                Namespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace" }
            };

            var wikiNamespaceRepository = new Mock<MvcWiki.Repository.IWikiNamespaceRepository>();

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiNamespaceRepository()).Returns(wikiNamespaceRepository.Object);

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var wikiPageRepository = new MvcWiki.Repository.FileSystem.WikiPageFileSystemRepository(repositoryFactory.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            unitOfWork.Setup(x => x.Saved(wikiPage, wikiPageRepository));

            wikiPageRepository.SaveWikiPage(wikiPage);

            unitOfWork.VerifyAll();
        }

        [Fact]
        public void Calls_IWikiNamespaceRepository_SaveWikiNamespace()
        {
            var wikiNamespace = new MvcWiki.Model.WikiNamespace { Name = "Namespace" };

            var wikiPage = new MvcWiki.Model.WikiPage
            {
                Name = "Page",
                Namespace = wikiNamespace
            };

            var wikiNamespaceRepository = new Mock<MvcWiki.Repository.IWikiNamespaceRepository>();
            wikiNamespaceRepository.Setup(x => x.SaveWikiNamespace(wikiNamespace));

            var repositoryFactory = new Mock<MvcWiki.Repository.IWikiRepositoryFactory>();
            repositoryFactory.Setup(x => x.GetWikiNamespaceRepository()).Returns(wikiNamespaceRepository.Object);

            var fileSystem = new Mock<MvcWiki.Repository.FileSystem.IFileSystemWrapper>();

            var unitOfWork = new Mock<MvcWiki.Repository.IUnitOfWork>();

            var wikiPageRepository = new MvcWiki.Repository.FileSystem.WikiPageFileSystemRepository(repositoryFactory.Object, unitOfWork.Object, "c:\\path", fileSystem.Object);

            unitOfWork.Setup(x => x.Saved(wikiPage, wikiPageRepository));

            wikiPageRepository.SaveWikiPage(wikiPage);

            wikiNamespaceRepository.VerifyAll();
        }
    }
}
